package com.fivetalks.client;

import java.io.IOException;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.ProgressBar;

import com.fivetalks.object.ChatMessage;

public class LoveRoomActivity extends Activity implements OnClickListener,
		Runnable {
	private ArrayList<ChatMessage> chatList;
	private ChatCustomAdapter mAdapter;
	private ListView list;
	Thread m_thread;
	static final int MAX = 15;
	public ProgressBar pbTimer;

	long roomNumber;
	ImageButton btSend,btOut;
	EditText etChat;

	String sendMsg;

	Handler mHandler;
	boolean isRun;
	
	DBConnect db;
	String email,nickname;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_love_room);
		chatList = new ArrayList<ChatMessage>();

		list = (ListView) findViewById(R.id.lvList);
		mAdapter = new ChatCustomAdapter(this, R.layout.chatting_row, chatList, "LoveRoomActivity");
		list.setAdapter(mAdapter);
		Intent intent = getIntent();
		roomNumber = (Long) intent.getSerializableExtra("roomNumber");
		btOut = (ImageButton) findViewById(R.id.btOut);
		btOut.setOnClickListener(this);
		db = DBConnect.open(this);
		
		try {
			Thread.sleep(500);
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		Thread m_thread_init = new Thread(new Runnable() {
			@Override
			public void run() {
				// 해당 작업을 처리함
				SocketClient sc = SocketClient.getInstance(null, null, null,
						null, null, null, null, LoveRoomActivity.this);
				try {
					sc.getDos().writeUTF("savePartner");
					sc.getDos().writeUTF(roomNumber + "");
					Log.d("love","savePartner 전송 ");
				} catch (IOException e) {
					e.printStackTrace();
				}

			}
		});

		m_thread_init.start();
		
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}


		Thread m_thread_go = new Thread(new Runnable() {
			@Override
			public void run() {
				// 해당 작업을 처리함
				SocketClient sc = SocketClient.getInstance(null, null, null,
						null, null, null, null, LoveRoomActivity.this);
				try {
					sc.getDos().writeUTF("playLove");
					sc.getDos().writeUTF(roomNumber + "");
					sendMsg = sc.getNickName() + "님이 입장하셨습니다.";
					sc.getDos().writeUTF(sendMsg);
					Log.d("chatting", "sendMsg : " + sendMsg);
					sc.getDos().flush();
				} catch (IOException e) {
					e.printStackTrace();
				}

			}
		});

		m_thread_go.start();

		btSend = (ImageButton) findViewById(R.id.btnSend);
		etChat = (EditText) findViewById(R.id.etChat);
		btSend.setOnClickListener(this);


	}

	@Override
	protected void onResume() {
		super.onResume();

	}

	public void start() {
		m_thread = new Thread(this);
		m_thread.start();
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	public void addResult(ChatMessage chat) {
		try {
//			Thread.sleep(400);
			chatList.add(chat);
		} catch (Exception e) {
			e.printStackTrace();
		}

		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				mAdapter.notifyDataSetChanged();
			}
		});
	}

	@Override
	public void run() {
		SocketClient sc = SocketClient.getInstance(null, null, null, null,
				null, null, null, LoveRoomActivity.this);
		try {
			sc.getDos().writeUTF("playLove");
			sc.getDos().writeUTF(roomNumber + "");
			sendMsg = etChat.getText().toString();

			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					// 해당 작업을 처리함
					etChat.setText("");
				}
			});
			sc.getDos().writeUTF(sendMsg);
			Log.d("chatting", "sendMsg : " + sendMsg);
			sc.getDos().flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}



	@Override
	public void onClick(View v) {
		switch (v.getId()) {

		case R.id.btnSend:

			start();
			break;
			
		case R.id.btOut :
			
			Thread m_thread_out = new Thread(new Runnable() {
				@Override
				public void run() {
					// 해당 작업을 처리함
					SocketClient sc = SocketClient.getInstance(null, null,
							null, null, null, null,null,LoveRoomActivity.this);
					try {
						sc.getDos().writeUTF("playLove");
						sc.getDos().writeUTF(roomNumber + "");
						sendMsg = sc.getNickName() + "님이 퇴장하셨습니다.";
						sc.getDos().writeUTF(sendMsg);
						Log.d("chatting", "sendMsg : " + sendMsg);
						sc.getDos().flush();
						
						
						
						sc.getDos().writeUTF("roomOut");
						sc.getDos().writeUTF(roomNumber + "");
						Log.d("prechat", "loveOut 전송1111");
						sc.getDos().flush();
						
						Intent intent = new Intent(LoveRoomActivity.this, MainFrameActivity.class);
						intent.putExtra("email", sc.getEmail());
						intent.putExtra("password", sc.getPassword());
						startActivity(intent);
					} catch (IOException e) {
						e.printStackTrace();
					}

				}
				
			});

			m_thread_out.start();
			
			
			
			
			
			
			
			break;
		}
	}

}
